package com.qidian.by.free.service.impl;

import com.qidian.by.free.service.FreeReceiveService;
import com.qidian.by.free.service.FreeTemplateService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Author: CoolPro
 * @Description:
 * @Time: 2024-05-17 21:09
 **/
@Service
public class FreeReceiveServiceImpl implements FreeReceiveService {

    @Resource
    private FreeTemplateService freeTemplateService;

    @Override
    public void onMessage(Message message, Channel channel) throws IOException {
//        获取消息
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            String msg = new String(message.getBody(), StandardCharsets.UTF_8);
//            确认信息
            channel.basicAck(deliveryTag,true);
            freeTemplateService.makeTemplate(msg);
        }catch (Exception e){
//            channel.basicAck(deliveryTag,false);
            System.out.println(e.getMessage());
        }
    }
}
